package com.hks360.car_treasure.listener;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.hks360.car_treasure.common.PrefKey;
import com.hks360.library.util.PrefUtil;
import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.UpdateManagerListener;

/**
 * Created by rwq on 2016/4/13.
 */
public class MyUpdateManagerListener extends UpdateManagerListener {
    private Context context;
    private Activity activity;
    private String result;
    private AppBean appBean;


    public MyUpdateManagerListener(Context context, Activity activity) {
        this.context = context;
        this.activity = activity;
    }

    @Override
    public void onNoUpdateAvailable() {
    }

    @Override
    public void onUpdateAvailable(String s) {
        // 将新版本信息封装到AppBean中
        final AppBean appBean = getAppBeanFromString(s);
        this.appBean = appBean;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("更新")
                .setMessage(appBean.getReleaseNote())
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PrefUtil.putBoolean(context, PrefKey.UPDATE, true);
                        startDownloadTask(activity, appBean.getDownloadURL());
                    }
                });
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                PrefUtil.putBoolean(context, PrefKey.UPDATE, false);
            }
        }).show();
    }
}
